package my.jwds.p36;

import com.alibaba.fastjson.JSONObject;

import java.util.HashSet;
import java.util.Set;

public class IsValidSudoku {

    public static void main(String[] args) {

        Solution2 obj = new Solution2();
        char[][]board = JSONObject.parseObject(
                "[[\".\",\".\",\"4\",\".\",\".\",\".\",\"6\",\"3\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\"5\",\".\",\".\",\".\",\".\",\".\",\".\",\"9\",\".\"],[\".\",\".\",\".\",\"5\",\"6\",\".\",\".\",\".\",\".\"],[\"4\",\".\",\"3\",\".\",\".\",\".\",\".\",\".\",\"1\"],[\".\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"5\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]"
                ,char[][].class);
        long start = System.currentTimeMillis();
        System.out.println(obj.isValidSudoku(board));
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    static class Solution {

        public boolean isValidSudoku(char[][] board) {
            Set<Character> set = new HashSet<>();
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (board[i][j] == '.')continue;
                    if (set.contains(board[i][j])){
                        return false;
                    }
                    set.add(board[i][j]);
                }
                set.clear();
                for (int j = 0; j < 9; j++) {
                    if (board[j][i] == '.')continue;
                    if (set.contains(board[j][i])){
                        return false;
                    }
                    set.add(board[j][i]);
                }
                set.clear();
                for (int j = 0; j < 3; j++) {
                    int x = (i%3)*3 + j;
                    for (int k = 0; k < 3; k++) {
                        int y = (i/3)*3 + k;
                        if (board[y][x] == '.')continue;
                        if (set.contains(board[y][x])){
                            return false;
                        }
                        set.add(board[y][x]);
                    }
                }
                set.clear();
            }
            return true;
        }
    }


    static class Solution1 {

        public boolean isValidSudoku(char[][] board) {
            Set<Character> setRow = new HashSet<>();
            Set<Character> setCol = new HashSet<>();
            Set<Character> SetChunk = new HashSet<>();
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (board[i][j] != '.'){
                        if (setRow.contains(board[i][j])){
                            return false;
                        }
                        setRow.add(board[i][j]);
                    }

                    if (board[j][i] != '.') {
                        if (setCol.contains(board[j][i])){
                            return false;
                        }
                        setCol.add(board[j][i]);
                    }
                }
                setRow.clear();
                setCol.clear();
                for (int j = 0; j < 3; j++) {
                    int x = (i%3)*3 + j;
                    for (int k = 0; k < 3; k++) {
                        int y = (i/3)*3 + k;
                        if (board[y][x] == '.')continue;
                        if (SetChunk.contains(board[y][x])){
                            return false;
                        }
                        SetChunk.add(board[y][x]);
                    }
                }
                SetChunk.clear();
            }
            return true;
        }
    }

    static class Solution2 {

        public boolean isValidSudoku(char[][] board) {

            int[][] setChunks = new int[3][10];
            for (int i = 0; i < 9; i++) {
                int[] setRow = new int[10];
                int[] setCol = new int[10];
                if (i % 3 == 0)setChunks = new int[3][10];
                for (int j = 0; j < 9; j++) {
                    if (board[i][j] != '.'){
                        if (setRow[board[i][j] - '0'] == 1){
                            return false;
                        }
                        setRow[board[i][j] - '0'] = 1;
                    }
                    if (board[j][i] != '.') {
                        if (setCol[board[j][i] - '0'] == 1){
                            return false;
                        }
                        setCol[board[j][i] - '0'] = 1;
                    }
                    if (board[i][j] != '.'){
                        int y = j / 3;
                        if (setChunks[y][board[i][j] - '0'] == 1){
                            return false;
                        }
                        setChunks[y][board[i][j] - '0'] = 1;

                    }

                }

            }
            return true;
        }
    }
}